//package scau.cty.interceptor;
//
//import com.alibaba.fastjson.JSON;
//import org.apache.logging.log4j.LogManager;
//import org.apache.logging.log4j.Logger;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;
//import scau.cty.base.redis.RedisCacheUtil;
//
//import scau.cty.dictionary.AuthCode;
//
//import javax.servlet.http.HttpServletRequest;
//import javax.servlet.http.HttpServletResponse;
//import java.io.IOException;
//import java.io.PrintWriter;
//import java.util.HashMap;
//import java.util.Map;
//
///**
// * Created by cty on 2017/3/12.
// */
//public class GlobalInterceptor extends HandlerInterceptorAdapter {
//    private Logger logger = LogManager.getLogger(this.getClass());
//    @Autowired
//    private IRedisService iRedisService;
//    @Autowired
//    private RedisCacheUtil<String> redisCacheUtil;
//    private final String BLACK = "black_";
//    private final String WHITE = "white_";
//    @Override
//    public boolean preHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object obj) throws Exception {
//        String ip = getIpAddress(httpServletRequest);
//
//        String blackIp = BLACK + ip;
//        String whiteIp = WHITE + ip;
////        //查找该IP是否在黑名单内
//        if (redisCacheUtil.getCacheObject(blackIp) != null) {
//            logger.warn("黑名单{}发起访问，已拒绝", blackIp);
//            iRedisService.updateBlackIp(blackIp);
//            rejectBlackIp(httpServletResponse);
//            return false;
//        }
////
////        //允许该IP访问
//        if (iRedisService.updateIpRate(whiteIp)) {
//            logger.info("{}在白名单内，允许访问",whiteIp);
//            return true;
//        }
////        //将IP列入黑名单
//        else {
//            iRedisService.addBlackIp(blackIp);
//            rejectBlackIp(httpServletResponse);
//            logger.warn("将{}列入黑名单", blackIp);
//            return false;
//        }
//
//
//    }
//
//    private void rejectBlackIp(HttpServletResponse httpServletResponse) {
//        try (PrintWriter out = httpServletResponse.getWriter()) {
//            httpServletResponse.setContentType("application/json");
//            httpServletResponse.setCharacterEncoding("utf-8");
//            Map map = new HashMap<>();
//            map.put("code", AuthCode.TOO_MANY_VISIT.getCode());
//            map.put("message", AuthCode.TOO_MANY_VISIT.getMessage());
//            out.print(JSON.toJSON(map));
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
//    }
//
//    private  String getIpAddress(HttpServletRequest request) {
//        String ip = request.getHeader("x-forwarded-for");
//        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
//            ip = request.getHeader("Proxy-Client-IP");
//        }
//        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
//            ip = request.getHeader("WL-Proxy-Client-IP");
//        }
//        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
//            ip = request.getHeader("HTTP_CLIENT_IP");
//        }
//        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
//            ip = request.getHeader("HTTP_X_FORWARDED_FOR");
//        }
//        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
//            ip = request.getRemoteAddr();
//        }
//        return ip;
//    }
//}
